#ifndef _EKOS_SPINLOCK_H_
#define _EKOS_SPINLOCK_H_

#include <inc/types.h>
#include <inc/x86.h>

static inline void sl_init(volatile void *sp) __attribute__((always_inline));
static inline bool sl_trylock(volatile void *sp) __attribute__((always_inline));
static inline void sl_spinlock(volatile void *sp) __attribute__((always_inline));
static inline void sl_release(volatile void *sp) __attribute__((always_inline));

static inline void
sl_init(volatile void *sp)
{
     *(uint8_t *)sp = 0;
}

static inline bool
sl_trylock(volatile void *sp)
{
     return (xchg8((uint8_t *)sp, 1) == 0);
}

static inline void
sl_spinlock(volatile void *sp)
{
     while (!sl_trylock(sp));
}

static inline void
sl_release(volatile void *sp)
{
     xchg8((uint8_t *)sp, 0);
}

#endif /* _EKOS_SPINLOCK_H_ */
